package com.jn.erp.security.sysservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.security.entity.SysUserRole;
import com.jn.erp.security.mapper.SysUserRoleMapper;
import com.jn.erp.security.sysservice.SysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/8/28 15:45
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleIds 角色组
     */
    public void insertUserRole(Long userId, Long[] roleIds,Long orgId)
    {
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                ur.setOrgId(orgId);
                list.add(ur);
            }
            this.saveBatch(list);
        }
    }

    @Override
    public List<SysUserRole> getRolesByUserOrgId(Long userId, Long orgId) {
        QueryWrapper<SysUserRole> qw=new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("org_id",orgId);
        return this.list(qw);
    }


}
